package com.ybzl.collect.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * 时间工具类
 *
 * @author dtf
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH = "yyyy-MM-dd hh24";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算相差天数
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
    }

    /**
     * 计算2个日期之间的相隔天数
     *
     * @param fromDate 起始日期
     * @param toDate   结束日期
     * @return 间隔天数
     */
    public static int getDays(String fromDate, String toDate) {
        return getDays(getDate(fromDate), getDate(toDate));
    }

    /**
     * 计算2个日期之间的相隔天数
     *
     * @param fromDate 起始日期
     * @param toDate   结束日期
     * @return 间隔天数
     */
    public static int getDays(Date fromDate, Date toDate) {
        long times = fromDate.getTime() - toDate.getTime();
        int days = (int) (times / (24 * 60 * 60 * 1000));
        return days;
    }

    /**
     * 计算时间差
     *
     * @param endDate   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     *  Date ==> LocalDateTime
     * @param date 时间类型
     */
    public static LocalDateTime dateConvertLocalDateTime(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     *  string ==> Date
     * @param time 时间类型字符串
     * @param format 日期格式
     */
    public static Date strConvertDate(String time, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * string ==> LocalDate， 时间字符串格式： 年月日 (日期转字符串是否可以长转短)
     * @param time 时间类型字符串
     */
    public static LocalDate strConvertLocalDate(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtil.YYYY_MM_DD);
        return LocalDate.parse(time, formatter);
    }
    /**
     *  LocalDate ==> string
     * @param localDate 时间类型字符串 (年月日)
     */
    public static String LocalDateConvertStr(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtil.YYYY_MM_DD);
        return localDate.format(formatter);
    }
    /**
     *  LocalDateTime ==> string
     * @param localDate 时间类型字符串 (年月日时分秒)
     */
    public static String LocalDateTimeConvertStr(LocalDateTime localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtil.YYYY_MM_DD_HH_MM_SS);
        return localDate.format(formatter);
    }
    /**
     * string ==> LocalDateTime
     * @param time 时间类型字符串
     * @param format 日期格式
     */
    public static LocalDateTime strConvertLocalDateTime(String time, String format){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, formatter);
    }
    /**
     *  计算本周的时间范围
     * @return
     */
    public static LocalDate[] calculateThisWeek(){
        return calculateThisWeek(LocalDate.now());
    }
    /**
     *  计算给定日期所处的周时间范围
     * @return
     */
    public static LocalDate[] calculateThisWeek(LocalDate localDate){
        LocalDate[] rangs = new LocalDate[2];
        LocalDate startOfWeek = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        rangs[0] =startOfWeek;
        rangs[1] =endOfWeek;
        return rangs;
    }
    /**
     *  计算本月的时间范围
     * @return
     */
    public static LocalDate[] calculateThisMonth(){
        return calculateThisMonth(LocalDate.now());
    }
    /**
     *  计算给定日期所处的月份时间范围
     * @return
     */
    public static LocalDate[] calculateThisMonth(LocalDate localDate){
        LocalDate[] rangs = new LocalDate[2];
        LocalDate startOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
        rangs[0] =startOfMonth;
        rangs[1] =endOfMonth;
        return rangs;
    }

    /**
     * 获取Date格式的日期信息
     *
     * @param date 日期（yyyy-MM-dd）
     * @return Date
     */
    public static Date getDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        if (date == null) {
            return null;
        } else {
            try {
                return simpleDateFormat.parse(date);
            } catch (Exception e) {
                return null;
            }
        }
    }

    /**
     * 获取String格式的日期信息（YYYY_MM_DD_HH_MM_SS）
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String getDateTime(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        if (date == null) {
            return "";
        } else {
            return simpleDateFormat.format(date);
        }
    }

    /**
     * 获取String格式的日期信息（yyyy-MM-dd）
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String getDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        if (date == null) {
            return "";
        } else {
            return simpleDateFormat.format(date);
        }
    }

    /**
     * 根据当前日期获取本周所有时间字符串
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static List<String> getDateListZ(Date date) {
        List<String> list = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
        LocalDate nowDay = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 使用周枚举分组计算得到当前日期对应周的7天日期
        List<LocalDate> weekDays = Arrays.stream(DayOfWeek.values()).map(nowDay::with).collect(toList());
        // 输出
        for (LocalDate w:weekDays) {
            String dateString = w.format(formatter);
            list.add(dateString);
        }
        return list;
    }

    /**
     * 根据当前日期获取本月所有时间字符串
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static List<String> getDateListY(Date date) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,0 );
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < max; i++) {
            calendar.set(Calendar.DAY_OF_MONTH, i + 1);
            String day = sdf.format(calendar.getTime());
            list.add(day);
        }
        return list;
    }

    /**
     * @author jieyuxing
     * @Description: 小时差
     * @Params:
     * @param: nowDate
     * @param: endDate
     * @Return: int
     */
    public static int getHourPoor(Date nowDate, Date endDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        int hourInt = (int) hour;
        if((int) day>0){
            hourInt = (int) day*24 + hourInt;
        }
        return hourInt;
    }

    //获取当前月的天数
    public static int getDaysOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取当前时间前七天时间
     * @return List<String>
     */
    public static List<String> getSevenDate() {

        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            int y = 6 -i;
            Date date = DateUtil.addDays(new Date(), -y);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    /**
     * 获取当前时间每小时集合时间
     * @return List<String>
     */
    public static List<String> getSevenHourList() {
        List<String> dateList = new ArrayList<>();
        String YMD = getTimeOffset(new Date(), 0, YYYY_MM_DD);
        String y ;
        for (int i = 0; i < 24; i++) {
            if(i < 10){
                y = "0" + i;
            }else{
                y = String.valueOf(i);
            }
            String formatDate = YMD + " " +y;
            dateList.add(formatDate);
        }
        return dateList;
    }

    /**
     * 获取当前时间天集合时间
     * @return List<String>
     */
    public static List<String> getSevenDateList() {
        String YMD = getTimeOffset(new Date(), 0, YYYY_MM);
        List<String> dateList = new ArrayList<>();
        int t = getDaysOfCurrentMonth();
        String y ;
        for (int i = 1; i < t+1; i++) {
            if(i < 10){
                y = "0" + i;
            }else{
                y = String.valueOf(i);
            }
            String formatDate = YMD + "-" + y;
            dateList.add(formatDate);
        }
        return dateList;
    }

    /**
     * 获取当前时间每月集合时间
     * @return List<String>
     */
    public static List<String> getSevenMonthsList() {
        String YMD = getTimeOffset(new Date(), 0, YYYY);
        List<String> dateList = new ArrayList<>();
        String y ;
        for (int i = 1; i < 13; i++) {
            if(i < 10){
                y = "0" + i;
            }else{
                y = String.valueOf(i);
            }
            String formatDate = YMD + "-" +y;
            dateList.add(formatDate);
        }
        return dateList;
    }

    /**
     * @author jieyuxing
     * @Description: 获取当前时间之前(天)的时间
     * @Params:
     * @param: date 时间
     * @param: mun 时间偏移量
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @Return: int
     */
    public static String getTimeOffset(Date date, int num, String timeType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -num);
        SimpleDateFormat df = new SimpleDateFormat(timeType);
        return df.format(calendar.getTime());
    }

    /**
     * @author jieyuxing
     * @Description: 获取当前时间之前(月)的时间
     * @Params:
     * @param: date 时间
     * @param: mun 时间偏移量
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @Return: int
     */
    public static String getTimeOffsetMonth(Date date, int num, String timeType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -num);
        SimpleDateFormat df = new SimpleDateFormat(timeType);
        return df.format(calendar.getTime());
    }

    /**
     * @author jieyuxing
     * @Description: 获取当前时间之前(年)的时间
     * @Params:
     * @param: date 时间
     * @param: mun 时间偏移量
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @Return: int
     */
    public static String getTimeOffsetYear(Date date, int num, String timeType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -num);
        SimpleDateFormat df = new SimpleDateFormat(timeType);
        return df.format(calendar.getTime());
    }

    /**
     * 获取今年/去年一个小时前的时间
     *
     * @param num 时间偏移量
     * @param start 需要获取的时间类型1：天；2:周；3:月；4：季度；5：年
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @param ble 是否是去年
     * @param date 需要操作的时间
     * @return
     */
    public static String getInteger(int num, Integer start, String timeType,boolean ble, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 控制分
        calendar.set(Calendar.MINUTE, 0);
        // 控制秒
        calendar.set(Calendar.SECOND, 0);
        /* HOUR_OF_DAY 指示一天中的小时 */
        //calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - num);
        calendar.add(Calendar.HOUR_OF_DAY,-num);

        switch (start) {
            // 昨天
            case 1:
                calendar.add(Calendar.DATE, -1);
                break;
            // 周
            case 2:
                calendar.add(Calendar.DATE, -7);
                break;
            // 月
            case 3:
                calendar.add(Calendar.MONTH, -1);
                break;
            // 季度
            case 4:
                calendar.add(Calendar.MONTH, -3);
                break;
            default:
                break;
        }
        if(ble){
            // 控制年
            calendar.add(Calendar.YEAR, -1);
        }
        SimpleDateFormat df = new SimpleDateFormat(timeType);
        return df.format(calendar.getTime());
    }

    /**
     * 获取前的时间、过去七天、过去一月、过去三个月、过去一年
     * @param start 需要获取的时间类型1：天；2:周；3:月；4：季度；5：年
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @param date 需要操作的时间
     */
    public static String getInteg(int start, String timeType, Date date) {
        SimpleDateFormat format = new SimpleDateFormat(timeType);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String dateTime;
        switch (start) {
            // 天
            case 1:
                calendar.add(Calendar.DATE, 0);
                break;
            // 周
            case 2:
                calendar.add(Calendar.DATE, - 7);
                break;
            // 月
            case 3:
                calendar.add(Calendar.MONTH, -1);
                break;
            // 季度
            case 4:
                calendar.add(Calendar.MONTH, -3);
                break;
            // 年
            case 5:
                calendar.add(Calendar.YEAR, -1);
                break;
            default:
                break;
        }
        Date d = calendar.getTime();
        dateTime = format.format(d);
        return dateTime;
        /*//现在日期
        String now = format.format(new Date());
        System.out.println("现在日期  ："+now);
        //过去七天
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, - 7);
        Date d = calendar.getTime();
        String day = format.format(d);
        System.out.println("过去七天  ："+day);

        //过去一月
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        Date m = calendar.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月："+mon);

        //过去三个月
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -3);
        Date m3 = calendar.getTime();
        String mon3 = format.format(m3);
        System.out.println("过去三个月："+mon3);

        //过去一年
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        Date y = calendar.getTime();
        String year = format.format(y);
        System.out.println("过去一年  ："+year);*/
    }

    /**
     * 获取（当前日期、本周第一天、本月第一天、本季度第一天、当前年第一天）
     * @param start 需要获取的时间类型1：天；2:周；3:月；4：季度；5：年
     * @param timeType 格式化时间（"yyyy-MM-dd";"yyyy-MM-dd HH:mm:ss"等等）
     * @param ble 是否是去年
     * @param date 需要操作的时间
     * @return
     */
    public static String getQsTime(int start, String timeType, boolean ble, Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(timeType);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (start) {
            case 1:
                calendar.add(Calendar.DATE, 0);
                break;
            // 周
            case 2:
                calendar.set(Calendar.DAY_OF_WEEK, 2);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                break;
            // 月
            case 3:
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                break;
            // 季度
            case 4:
                int nowMonth = calendar.get(Calendar.MONTH);
                calendar.set(Calendar.MONTH, nowMonth - (nowMonth % 3));
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                break;
            // 年
            case 5:
                calendar.set(Calendar.DAY_OF_YEAR, 1);
                calendar.set(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                break;
            default:
                break;
        }
        if(ble){
            // 控制年
            calendar.add(Calendar.YEAR, -1);
        }
        return dateFormat.format(calendar.getTime());
        /*calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String mon = dateFormat.format(calendar.getTime());
        System.out.println("获取本周第一天"+mon);

        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String month = dateFormat.format(calendar.getTime());
        System.out.println("获取本月第一天"+month);

        calendar.setTime(date);
        int nowMonth = calendar.get(Calendar.MONTH) + 1;
        calendar.set(Calendar.MONTH, nowMonth - (nowMonth % 3));
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String quarter = dateFormat.format(calendar.getTime());
        System.out.println("获取本季度第一天"+quarter);

        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String year = dateFormat.format(calendar.getTime());
        System.out.println("获取本年第一天"+year);*/
    }

    /**
     * 获取指定日期
     *
     * @param num 偏移量
     * @param dateType 日期类型 0-日 1-月 2-年
     * @return
     */
    public static LocalDate getGoalDate(Integer num, Integer dateType) {
        LocalDate localDate = null;
        if (dateType == 0) {
            localDate = LocalDate.now().plus(num, ChronoUnit.DAYS);
        } else if (dateType == 1) {
            localDate = LocalDate.now().plus(num, ChronoUnit.MONTHS);
        } else if (dateType == 2) {
            localDate = LocalDate.now().plus(num, ChronoUnit.YEARS);
        }
        return localDate;
    }


    /** 参入指定日期值 获取日期的偏移量
     * @Author: hpz
     * @Date: 2020/10/16 9:36
     **/
    public static LocalDate getGoalDate(LocalDate designatedDate ,Integer num, Integer dateType) {
        LocalDate localDate = null;
        if (dateType == 0) {

            localDate = designatedDate.plus(num, ChronoUnit.DAYS);
        } else if (dateType == 1) {
            localDate = designatedDate.plus(num, ChronoUnit.MONTHS);
        } else if (dateType == 2) {
            localDate = designatedDate.plus(num, ChronoUnit.YEARS);
        }else if (dateType == 4) {
            localDate = designatedDate.plus(num,ChronoUnit.WEEKS);
        }
        return localDate;
    }

    /**
     * 获取某季度的开始日期 季度一年四季， 第一季度：2月-4月， 第二季度：5月-7月， 第三季度：8月-10月， 第四季度：11月-1月
     *
     * @param offset 0本季度，1下个季度，-1上个季度，依次类推
     * @return
     */
    public static String quarterStart(int offset) {
        final LocalDate date = LocalDate.now().plusMonths(offset * 3);
        int year = date.getYear();
        int month = date.getMonth().getValue(); // 当月
        int quarter = 1;
        if (month >= 1 && month <= 3) { // 第一季度
            quarter = 1;
        } else if (month >= 4 && month <= 6) { // 第二季度
            quarter = 2;
        } else if (month >= 7 && month <= 9) { // 第三季度
            quarter = 3;
        } else if ((month >= 10 && month <= 12)) { // 第四季度
            quarter = 4;
        } else if (month == 1) { // 第四季度
            quarter = 4;
        }
        String quarterStr = year + "-" + quarter;
        return quarterStr;
    }

    /**
     * @Author jieyuxing
     * @Description: 计算两个日期之间的分钟差
     * @Params:
     * @param: start
     * @param: end
     * @Return:
     * @Throws:
     * @Date: Created in 17:40 2018/6/8
     */
    public static String getTimeSubtraction(Date before, Date after) {

        //起始时间毫秒数
        Calendar beforeCalendar = Calendar.getInstance();
        beforeCalendar.setTime(before);
        long beforeTime = beforeCalendar.getTimeInMillis();


        //截止时间毫秒数
        Calendar afterCalendar = Calendar.getInstance();
        afterCalendar.setTime(after);
        long afterTime = afterCalendar.getTimeInMillis();

        //获取时间差(毫秒数量)
        long timeCut = afterTime - beforeTime;

        //将毫秒转为分钟
        long time = timeCut / 1000 / 60;

        return String.valueOf(time);
    }

    /**
     * 一段时间内日期列表
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getDaysBetweenDates(Date startDate, Date endDate) {
        List<String> dates = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        while (startDate.getTime() <= endDate.getTime()) {
            dates.add(sdf.format(startDate));
            start.add(Calendar.DAY_OF_MONTH, 1);
            startDate = start.getTime();
        }
        return dates;
    }

    /**
     * 当前时间往前推几天
     * @param d 当前时间
     * @param day  前推天数
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
        return calendar.getTime();
    }
}
